/**
 * Copyright 31.08.2012 Alex Vikulov (vikuloff@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.stockchart.core;

import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Region.Op;

/**
 * @author alexv
 *
 */
public abstract class ChartElement 
{
	private final RectF fTempRect = new RectF();

	private final ChartElement fParent;
	
	private final RectF fRectF = new RectF();
	
	public ChartElement(ChartElement parent)
	{
		fParent = parent;		
	}
	
	public void draw(Canvas c)
	{
		preDraw(c);
		
		onDraw(c);
		
		postDraw(c);
	}
	
	private void preDraw(Canvas c)
	{
		c.save();
		c.translate(fRectF.left, fRectF.top);
		
		fTempRect.set(0,0,fRectF.width(),fRectF.height());
		c.clipRect(fTempRect, Op.REPLACE);		
	}
	
	private void postDraw(Canvas c)
	{
		c.restore();
	}
	
	protected abstract void onDraw(Canvas c);
	
	protected void onBoundsChanged() { }
	
	public ChartElement getParent()
	{
		return fParent;
	}
	public void setBounds(RectF r)
	{
		fRectF.set(r);
	}
	
	public float width()
	{
		return fRectF.width();
	}
	
	public float height()
	{
		return fRectF.height();
	}
	
	public void setBounds(float relX,float relY, float width,float height)
	{
		fRectF.left = relX;
		fRectF.top = relY;
		fRectF.right = fRectF.left + width;
		fRectF.bottom = fRectF.top + height;

		onBoundsChanged();
	}
	
	public RectF getBounds()
	{
		return fRectF;
	}
	
	public PointF getAbsolutePosition(float x,float y)
	{
		ChartElement parent = this;
		
		do
		{
			x+=parent.fRectF.left;
			y+=parent.fRectF.top;
			
			parent = parent.getParent();
		}
		while(parent != null);
		
		return new PointF(x,y);
	}
	public RectF getAbsoluteBounds()
	{
		PointF ap = (getParent() != null?getParent().getAbsolutePosition(fRectF.left,fRectF.top):new PointF(fRectF.left,fRectF.top));
		
		return new RectF(ap.x,ap.y,ap.x+fRectF.width(),ap.y+fRectF.height());		
	}

}
